//
//  GuideViewController.m
//  FlyGift
//
//  Created by lanou3g on 15/7/14.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "GuideViewController.h"

@interface GuideViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    CGFloat contentHeight;
}
// 存放频道的小数组
@property(nonatomic, strong)NSMutableArray *channelsModelArray;
// 存放大数组
@property(nonatomic, strong)NSMutableArray *groupsArray;
// 显示分区头的数组
@property(nonatomic, strong)NSMutableArray *headerArray;
// 存放两个分区的滚动视图
@property(nonatomic, strong)UIScrollView *scrollViewContainer;
@end


@implementation GuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 调用解析方法
    [self guideSortDataRequest];
    // 添加banner的集合视图
    self.bannerView = [[BannerView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kP6Height(150))];
}

-(void)loadScrollView
{
    // 分组频道的集合视图
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.guideCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, kP6Height(160), kScreenWidth, contentHeight) collectionViewLayout:flowLayout];
    self.guideCollectionView.delegate = self;
    self.guideCollectionView.dataSource = self;
    self.guideCollectionView.backgroundColor = [UIColor whiteColor];
    self.guideCollectionView.scrollEnabled = NO;
    // 注册表头
    [self.guideCollectionView registerClass:[GuideHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"GuideHeader"];
    NSLog(@"%f",flowLayout.collectionViewContentSize.height);
    self.scrollViewContainer = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kP6Height(64))];
    self.scrollViewContainer.contentSize = CGSizeMake(kScreenWidth, self.bannerView.frame.size.height + flowLayout.collectionViewContentSize.height + kP6Height(64));
    self.scrollViewContainer.contentOffset = CGPointMake(0, 0);
    [self.view addSubview:_scrollViewContainer];
    [self.scrollViewContainer addSubview:_bannerView];
    [self.scrollViewContainer addSubview:_guideCollectionView];
}



#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(kP6width(60), kP6Height(80));
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(kP6width(10), kP6width(10), kP6width(10), kP6width(10));
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return kP6width(15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return kP6width(15);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(kScreenWidth, kP6Height(30));
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *array = self.groupsArray[section];
    return array.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *guideCell = @"GuideCell";
    [collectionView registerClass:[ImageViewLabelCollectionViewCell class] forCellWithReuseIdentifier:guideCell];
    ImageViewLabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:guideCell forIndexPath:indexPath];
    ChannelsModel *model = self.groupsArray[indexPath.section][indexPath.row];
    cell.giftNameLabel.text = model.name;
    [cell.giftImageView setImageWithURL:[NSURL URLWithString:model.icon_url]];
    cell.giftImageView.layer.cornerRadius = cell.frame.size.width / 2;
    cell.giftImageView.layer.masksToBounds = YES;
    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.groupsArray.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        GuideHeaderCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"GuideHeader" forIndexPath:indexPath];
        NSString *strName = self.headerArray[indexPath.section];
        view.guideTitleLabel.text = strName;
        return view;
    }
    else
    {
        return nil;
    }
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ChannelsModel *model = self.groupsArray[indexPath.section][indexPath.row];
    
    if (self.clickChannelItemBlock == nil)
    {
        return;
    }
    self.clickChannelItemBlock(model.channelId);
    
}
#pragma mark - 攻略分类数据部分
- (void)guideSortDataRequest
{
    self.groupsArray = [NSMutableArray array];
    [LORequestManger GET:GuideSortURL success:^(id response) {
        
        if (response == nil)
        {
            return;
        }
        NSDictionary *dictionary = (NSDictionary *)response;
        NSDictionary *dataDic = dictionary[@"data"];
        NSArray *groupsArray = dataDic[@"channel_groups"];
        self.headerArray = [NSMutableArray array];
        for (NSDictionary *groupDic in groupsArray)
        {
            [self.headerArray addObject:groupDic[@"name"]];
            
            self.channelsModelArray = [NSMutableArray array];
            NSArray *channelsArray = groupDic[@"channels"];
            for (NSDictionary *channelsDic in channelsArray)
            {
                ChannelsModel *model = [[ChannelsModel alloc]init];
                model.icon_url = channelsDic[@"icon_url"];
                model.name = channelsDic[@"name"];
                model.group_id = channelsDic[@"group_id"];
                model.channelId = [channelsDic[@"id"] stringValue];
                [self.channelsModelArray addObject:model];
            }
            
            [self.groupsArray addObject:self.channelsModelArray];
        }
        
        // 计算collectionView的总内容的高度
        // 记录行数的
        unsigned long row = 0;
        for (int i = 0; i < self.groupsArray.count; i++)
        {
            NSArray * arr = self.groupsArray[i];
            if (arr.count % 4 != 0) {
                row = arr.count / 4 + 1;
            }
            else         
            {
                row = arr.count / 4;
            }
            // cell的高度 + 表头的高度 + 行间距 * (行数 - 1) + 上下间距
            contentHeight += row* kP6Height(80) + kP6Height(30) + 15 * (row - 1) + 20;
        }
        [self loadScrollView];
        [self.guideCollectionView reloadData];
    } failure:nil];
}


@end
